package com.cmbc.xiaodu.dpl;

import com.baidu.dueros.bot.BaseBot;
import com.baidu.dueros.data.request.IntentRequest;
import com.baidu.dueros.data.request.LaunchRequest;
import com.baidu.dueros.data.response.OutputSpeech;
import com.baidu.dueros.data.response.directive.dpl.Document;
import com.baidu.dueros.data.response.directive.dpl.ExecuteCommands;
import com.baidu.dueros.data.response.directive.dpl.RenderDocument;
import com.baidu.dueros.data.response.directive.dpl.commands.*;
import com.baidu.dueros.data.response.directive.dpl.event.UserEvent;
import com.baidu.dueros.model.Response;
import com.fasterxml.jackson.databind.JsonMappingException;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @Author: Mr.xie
 * @Date: 2020/08/26 17:23
 * @Description: TODO
 */
public class Dpl_Bot extends BaseBot {

    protected Dpl_Bot(HttpServletRequest request) throws IOException, JsonMappingException {
        super(request);
    }

    protected Response onLaunch(LaunchRequest launchRequest) {

        try {
            // 从json文件中初始化Document
            Document document = Document.getDocumentFromPath("json/launch.json");

            // RenderDocument指令
            RenderDocument renderDocument = new RenderDocument();
            renderDocument.setDocument(document);

            this.addDirective(renderDocument);

            OutputSpeech outputSpeech = new OutputSpeech(OutputSpeech.SpeechType.PlainText, "欢迎来到测试DPL");
            Response response = new Response(outputSpeech);
            return response;

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return null;
    }

    protected Response onInent(IntentRequest intentRequest) {
        if ("dpl_demo1".equals(intentRequest.getIntentName())) {
            try {
                // 从json文件中初始化Document
                Document document = Document.getDocumentFromPath("F:\\minshengWlw\\xiaodu_minsheng\\src\\main\\java\\com\\cmbc\\xiaodu\\dpl\\demo1.json");

                // RenderDocument指令
                RenderDocument renderDocument = new RenderDocument();
                renderDocument.setDocument(document);

                this.addDirective(renderDocument);

                OutputSpeech outputSpeech = new OutputSpeech(OutputSpeech.SpeechType.PlainText, "简单图片");
                Response response = new Response(outputSpeech);
                return response;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else if ("dpl_demo2".equals(intentRequest.getIntentName())) {
            try {
                // 从json文件中初始化Document
                Document document = Document.getDocumentFromPath("F:\\minshengWlw\\xiaodu_minsheng\\src\\main\\java\\com\\cmbc\\xiaodu\\dpl\\demo2.json");

                // RenderDocument指令
                RenderDocument renderDocument = new RenderDocument();
                renderDocument.setDocument(document);

                this.addDirective(renderDocument);

                OutputSpeech outputSpeech = new OutputSpeech(OutputSpeech.SpeechType.PlainText, "长文本");
                Response response = new Response(outputSpeech);
                return response;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else if ("dpl_demo3".equals(intentRequest.getIntentName())) {
            try {
                // 从json文件中初始化Document
                Document document = Document.getDocumentFromPath("F:\\minshengWlw\\xiaodu_minsheng\\src\\main\\java\\com\\cmbc\\xiaodu\\dpl\\demo3.json");

                // RenderDocument指令
                RenderDocument renderDocument = new RenderDocument();
                renderDocument.setDocument(document);

                this.addDirective(renderDocument);

                OutputSpeech outputSpeech = new OutputSpeech(OutputSpeech.SpeechType.PlainText, "短文本");
                Response response = new Response(outputSpeech);
                return response;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else if ("dpl_demo4".equals(intentRequest.getIntentName())) {
            try {
                // 从json文件中初始化Document
                Document document = Document.getDocumentFromPath("F:\\minshengWlw\\xiaodu_minsheng\\src\\main\\java\\com\\cmbc\\xiaodu\\dpl\\demo4.json");

                // RenderDocument指令
                RenderDocument renderDocument = new RenderDocument();
                renderDocument.setDocument(document);

                this.addDirective(renderDocument);

                OutputSpeech outputSpeech = new OutputSpeech(OutputSpeech.SpeechType.PlainText, "右图详情");
                Response response = new Response(outputSpeech);
                return response;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else if ("dpl_demo5".equals(intentRequest.getIntentName())) {
            try {
                // 从json文件中初始化Document
                Document document = Document.getDocumentFromPath("F:\\minshengWlw\\xiaodu_minsheng\\src\\main\\java\\com\\cmbc\\xiaodu\\dpl\\demo5.json");

                // RenderDocument指令
                RenderDocument renderDocument = new RenderDocument();
                renderDocument.setDocument(document);

                this.addDirective(renderDocument);

                OutputSpeech outputSpeech = new OutputSpeech(OutputSpeech.SpeechType.PlainText, "左图详情");
                Response response = new Response(outputSpeech);
                return response;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else if ("dpl_demo6".equals(intentRequest.getIntentName())) {

            try {
                // 从json文件中初始化Document
                Document document = Document.getDocumentFromPath("F:\\minshengWlw\\xiaodu_minsheng\\src\\main\\java\\com\\cmbc\\xiaodu\\dpl\\demo6.json");

                // RenderDocument指令
                RenderDocument renderDocument = new RenderDocument();
                renderDocument.setDocument(document);

                this.addDirective(renderDocument);

                OutputSpeech outputSpeech = new OutputSpeech(OutputSpeech.SpeechType.PlainText, "横向列表");
                Response response = new Response(outputSpeech);
                return response;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else if ("dpl_demo7".equals(intentRequest.getIntentName())) {

            try {
                // 从json文件中初始化Document
                Document document = Document.getDocumentFromPath("F:\\minshengWlw\\xiaodu_minsheng\\src\\main\\java\\com\\cmbc\\xiaodu\\dpl\\demo7.json");

                // RenderDocument指令
                RenderDocument renderDocument = new RenderDocument();
                renderDocument.setDocument(document);

                this.addDirective(renderDocument);

                OutputSpeech outputSpeech = new OutputSpeech(OutputSpeech.SpeechType.PlainText, "视频相册");
                Response response = new Response(outputSpeech);
                return response;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else if ("pull_scrollview".equals(intentRequest.getIntentName())) {
            ExecuteCommands executeCommands = new ExecuteCommands();
            ScrollCommand scrollCommand = new ScrollCommand();
            scrollCommand.setDistance("200dp").setComponentId("demo_pull_scrollview_compid");
            executeCommands.addCommand(scrollCommand);
            this.addDirective(executeCommands);
            OutputSpeech outputSpeech = new OutputSpeech(OutputSpeech.SpeechType.PlainText, "滑动窗口滑动");
            Response response = new Response(outputSpeech);
            return response;
        } else /*if ("video_play".equals(intentRequest.getIntentName())) {
            try {
                // 从json文件中初始化Document
                Document document = Document.getDocumentFromPath("com/cmbc/xiaodu/dpl/update.json");

                String index = this.getSlot("index");
                int i = Integer.parseInt(index);
                i = (i - 1) > 0 ? (i - 1) : 0;

                Video video = videoList.get(i);

                Object mainTemplate = document.getMainTemplate();
                ObjectMapper objectMapper = new ObjectMapper();
                JsonNode tree = objectMapper.readTree(objectMapper.writeValueAsString(mainTemplate));

                List<JsonNode> jsonNodes = tree.findValues("items").get(0).findValues("items");

                // 修改返回
                ((ObjectNode) (jsonNodes.get(0).get(0))).put("src", video.getSrc());
                ((ObjectNode) (jsonNodes.get(0).get(1).findValue("items").get(0))).put("text", video.getName());

                document.setMainTemplate(objectMapper.readValue(objectMapper.writeValueAsString(tree), Object.class));

                ExecuteCommands executeCommands = new ExecuteCommands();
                UpdateComponentCommand updateComponentCommand = new UpdateComponentCommand();
                updateComponentCommand.setComponentId("replaceComponentId");
                updateComponentCommand.setDocument(document);
                executeCommands.addCommand(updateComponentCommand);
                this.addDirective(executeCommands);

                OutputSpeech outputSpeech = new OutputSpeech(OutputSpeech.SpeechType.PlainText, "正在播放" + videoList.get(i).getName());
                Response response = new Response(outputSpeech);
                return response;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        } else*/ if ("move_list".equals(intentRequest.getIntentName())) {
            // 向上滑动列表
            String direction = this.getSlot("direction") != null ? this.getSlot("direction") : "下";
            String distance = "100dp";

            if (direction == "后" || direction == "下") {
                distance = "-100dp";
            }

            ExecuteCommands executeCommands = new ExecuteCommands();
            ScrollCommand scrollCommand = new ScrollCommand();
            scrollCommand.setComponentId("demo_list_compid");
            scrollCommand.setDistance(distance);
            executeCommands.addCommand(scrollCommand);

            this.addDirective(executeCommands);
            OutputSpeech outputSpeech = new OutputSpeech(OutputSpeech.SpeechType.PlainText, "向" + direction + "滑动列表");
            Response response = new Response(outputSpeech);
            return response;
        } else if ("go_list_top".equals(intentRequest.getIntentName())) {
            // 回到列表顶部
            ExecuteCommands executeCommands = new ExecuteCommands();
            ScrollToIndexCommand scrollToIndexCommand = new ScrollToIndexCommand();
            scrollToIndexCommand.setAlign(ScrollToIndexCommand.AlignType.FIRST).setIndex(1).setComponentId("demo_list_compid");
            executeCommands.addCommand(scrollToIndexCommand);
            this.addDirective(executeCommands);
            OutputSpeech outputSpeech = new OutputSpeech(OutputSpeech.SpeechType.PlainText, "回到列表顶部");
            Response response = new Response(outputSpeech);
            return response;
        } else if ("pause_video".equals(intentRequest.getIntentName())) {
            // 视频暂停
            ExecuteCommands executeCommands = new ExecuteCommands();

            ControlMediaCommand controlMediaCommand = new ControlMediaCommand();
            controlMediaCommand.setComponentId("demo_video_compid");
            controlMediaCommand.setCommand(ControlMediaCommand.ControlMediaCommandType.PAUSE);
            executeCommands.addCommand(controlMediaCommand);
            this.addDirective(executeCommands);
            OutputSpeech outputSpeech = new OutputSpeech(OutputSpeech.SpeechType.PlainText, "视频暂停播放");
            Response response = new Response(outputSpeech);
            return response;
        } else if ("video_continue".equals(intentRequest.getIntentName())) {
            // 视频继续播放
            ExecuteCommands executeCommands = new ExecuteCommands();
            ControlMediaCommand controlMediaCommand = new ControlMediaCommand();
            controlMediaCommand.setCommand(ControlMediaCommand.ControlMediaCommandType.PLAY).setComponentId("demo_video_compid");
            executeCommands.addCommand(controlMediaCommand);
            this.addDirective(executeCommands);
            OutputSpeech outputSpeech = new OutputSpeech(OutputSpeech.SpeechType.PlainText, "视频继续播放");
            Response response = new Response(outputSpeech);
            return response;
        } else if ("favourite_video".equals(intentRequest.getIntentName())) {
            // 收藏
            ExecuteCommands executeCommands = new ExecuteCommands();
            SetStateCommand setStateCommand = new SetStateCommand();
            setStateCommand.setState("src").setValue("https://dbp-dict.bj.bcebos.com/dpl%2F%E5%BF%83.png")
                    .setComponentId("demo_image_compid");

            AnimationCommand animationCommand = new AnimationCommand();
            animationCommand.setFrom("40dp").setTo("10dp").setEasing(AnimationCommand.EasingType.EASEIN).setAttribute("height")
                    .setDuration(500).setRepeatCount("9").setRepeatMode(AnimationCommand.RepeatMode.REVERSE)
                    .setComponentId("demo_image_compid");
            executeCommands.addCommand(setStateCommand).addCommand(animationCommand);
            this.addDirective(executeCommands);
            OutputSpeech outputSpeech = new OutputSpeech(OutputSpeech.SpeechType.PlainText, "视频收藏");
            Response response = new Response(outputSpeech);
            return response;

        } else if ("move_page".equals(intentRequest.getIntentName())) {
            // 翻页
            String direction = this.getSlot("direction") != null ? this.getSlot("dire" + "ction") : "下";
            int value = 1;
            if (direction == "左") {
                value = -1;
            }

            ExecuteCommands executeCommands = new ExecuteCommands();

            SetPageCommand setPageCommand = new SetPageCommand();
            setPageCommand.setPosition(SetPageCommand.Position.RELATIVE).setValue(value).setComponentId("demo_move_page_compid");
            executeCommands.addCommand(setPageCommand);
            this.addDirective(executeCommands);
            OutputSpeech outputSpeech = new OutputSpeech(OutputSpeech.SpeechType.PlainText, "向" + direction + "翻页");
            Response response = new Response(outputSpeech);
            return response;
        }
        return null;
    }

    protected Response onUserEvent(final UserEvent userEvent) {
        String type = userEvent.getPayload().getSource().getType();
        ExecuteCommands executeCommands = new ExecuteCommands();

        if ("Pager".equals(type)) {
            ControlMediaCommand controlMediaCommand = new ControlMediaCommand();
            controlMediaCommand.setComponentId("demo_video_compid");
            if ("0".equals(userEvent.getPayload().getSource().getValue())
                    || "2".equals(userEvent.getPayload().getSource().getValue())) {
                controlMediaCommand.setCommand(ControlMediaCommand.ControlMediaCommandType.PAUSE);
            }
            if ("1".equals(userEvent.getPayload().getSource().getValue())) {
                controlMediaCommand.setCommand(ControlMediaCommand.ControlMediaCommandType.PLAY);
            }
            executeCommands.addCommand(controlMediaCommand);
        }
        this.addDirective(executeCommands);

        Response response = new Response();
        return response;
    }
}
